<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryMutliAPI, getHomeHotMutliAPI } from '@/apis/home'
import type { GetHomeBannerItem, GetHomeCategoryMutliItem, GetHomeHotMutliItem } from '@/types/home'
import { ref } from 'vue'
import CategoryPanel from './components/CategoryPanel/CategoryPanel.vue'
import HotPanel from './components/HotPanel/HotPanel.vue'
import type { XtxGuessInstance } from '@/types/component'
import PageSkeleton from './components/PageSkeleton.vue'
import { onLoad } from '@dcloudio/uni-app'

const bannerList = ref<GetHomeBannerItem[]>([])
const getHomeBanner = async () => {
  const res = await getHomeBannerAPI(1)
  console.log('轮播图', res)
  bannerList.value = res.result
}
const categoryList = ref<GetHomeCategoryMutliItem[]>([])
const getHomeCategoryMutli = async () => {
  const res = await getHomeCategoryMutliAPI()
  console.log('前台分类', res)
  categoryList.value = res.result
}
const hotList = ref<GetHomeHotMutliItem[]>([])
const getHomeHotMutli = async () => {
  const res = await getHomeHotMutliAPI()
  console.log('热门推荐', res)
  hotList.value = res.result
}
// 猜你喜欢
const guessRef = ref<XtxGuessInstance>()
// 触底事件
const handleScrollToLower = () => {
  guessRef.value?.getMore()
}
const isLoading = ref(true)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBanner(), getHomeCategoryMutli(), getHomeHotMutli()])
  isLoading.value = false
})

// 下拉刷新
const isTriggered = ref(false)
const handleRefresherRefresh = async () => {
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([getHomeBanner(), getHomeCategoryMutli(), getHomeHotMutli()])
  isTriggered.value = false
}
</script>

<template>
  <custom-navbar></custom-navbar>
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="handleScrollToLower"
    refresher-enabled
    :refresher-triggered="isTriggered"
    @refresherrefresh="handleRefresherRefresh"
  >
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <template v-else>
      <xtx-swiper :list="bannerList"></xtx-swiper>
      <category-panel :list="categoryList"></category-panel>
      <hot-panel :list="hotList"></hot-panel>
      <xtx-guess ref="guessRef"></xtx-guess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
// 首页
page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  .scroll-view {
    flex: 1;
  }
}
</style>
